<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="font-size:20px;"><strong>类别分类</strong></span>
        <el-button type="primary" @click="addDialog = true">添加分类</el-button>
      </div>
      <el-table :data="list" style="width: 100%" stripe border>
        <el-table-column prop="id" label="ID" width="150"></el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="180"></el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
        <el-table-column label="操作">
           <template v-slot="obj">
            <el-button
              size="mini" @click="compile(obj.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="del(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加对话框 -->
    <el-dialog :before-close="cancel" title="添加分类" :visible.sync="addDialog" width="500px">
      <el-form label-width="80px" :model="addData" :rules="rules" ref="addForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off"  v-model="addData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="addData.cate_alias" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addList">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑对话框 -->
    <el-dialog :before-close="cancelOne" title="添加分类" :visible.sync="aditDialog" width="500px">
      <el-form label-width="80px" :model="aditData" :rules="rules" ref="aditForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="aditData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="aditData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelOne">取 消</el-button>
        <el-button type="primary" @click="updateList">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getListAPI, addListAPI, delListAPI, updateListAPI } from '@/api/category'
export default {
  name: 'CateList',
  data() {
    return {
      list: [],
      // 添加显示隐藏
      addDialog: false,
      // 编辑显示隐藏
      aditDialog: false,
      // 添加数据项
      addData: {},
      // 编辑数据项
      aditData: {
        cate_name: '',
        cate_alias: ''
      },
      rules: {
        cate_name: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '只能是长度1~15位的字母数字组合', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    // 获取列表
    async getList() {
      const { data: res } = await getListAPI()
      if (res.code === 0) {
        this.list = res.data
      }
    },
    // 点击确认添加列表
    addList() {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await addListAPI(this.addData)
        if (res.code === 0) {
        // 提示
          this.$message.success(res.message)
          this.$refs.addForm.resetFields()
          this.addDialog = false
          this.getList()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 点击删除
    del(id) {
      // console.log(id)
      this.$confirm('确认删除此文件吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await delListAPI(id)
        if (res.code === 0) {
          // 成功提示
          this.$message.success(res.message)
          // 重新渲染
          this.getList()
        } else {
          this.$message.error(res.message)
        }
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 操作点击取消
    cancel() {
      this.$refs.addForm.resetFields()
      this.addDialog = false
    },
    cancelOne() {
      this.$refs.aditForm.resetFields()
      this.aditDialog = false
    },
    // 显示编辑弹窗
    compile(row) {
      // row 就是正在编辑的这条数据
      this.editData = { ...row }
      this.aditDialog = true
    },
    updateList() {
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await updateListAPI(this.editData)
        if (res.code === 0) {
          this.$message.success(res.message)// 提示
          this.getCategory() // 重新获取数据，重新更新页面
          this.editDialog = false // 关闭弹出层
        } else {
          this.$message.error(res.message)
        }
      })
    }
  },

  // 生命周期函数
  created() {
    this.getList()
  }
}
</script>

<style>
.clearfix{
  display: flex;
  justify-content: space-between;
  align-content: center;
}
</style>
